﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 一键生成批处理文件逻辑代码
/// </summary>
public partial class ESF_Logic
{
    #region GetBatFilePath 获得指定类别的Bat文件路径
    /// <summary>
    /// 获得指定类别的Bat文件路径
    /// </summary>
    /// <param name="_bat">bat类别</param>
    /// <returns>Bat文件路径</returns>
    public static string GetBatFilePath(ESF_enBatClassify _bat)
    {
        return ESF_Utility.GetPath(_bat.ToString(), projectSetting.xml.editorBatFolder, enESF_UnityFileExt.Bat);
    }
    #endregion

    #region GetBatCodeTemplete 获得指定类别的Bat文件模板
    /// <summary>
    /// 获得指定类别的Bat文件模板
    /// </summary>
    /// <param name="_bat">bat类别</param>
    /// <returns>Bat文件模板</returns>
    public static string GetBatCodeTemplete(ESF_enBatClassify _bat)
    {
        string result = string.Empty;
        switch (_bat)
        {
            case ESF_enBatClassify.ClearPackageManifest:
                result = File.ReadAllText(ESF_enResxTempleteFile.Bat_ClearPackageManifest_Templete_txt);
                break;
            case ESF_enBatClassify.PlayerLog:
                result = File.ReadAllText(ESF_enResxTempleteFile.Bat_PlayerLog_Templete_txt);
                break;
            case ESF_enBatClassify.DebugProfiler:
                result = File.ReadAllText(ESF_enResxTempleteFile.Bat_DebugProfiler_Templete_txt);
                break;
        }
        return result;
    }
    #endregion

    #region GetClearPackageManifestBatCode 获得ClearPackageManifest批处理文件代码
    /// <summary>
    /// 获得ClearPackageManifest批处理文件代码
    /// </summary>
    /// <returns>Bat代码</returns>
    public static string GetClearPackageManifestBatCode(string _packageOutputPath)
    {
        return OnGetBatCode(ESF_enBatClassify.ClearPackageManifest, _packageOutputPath);
    }
    #endregion

    #region OnGetBatCode 获得指定类型的批处理文件代码
    /// <summary>
    /// 获得指定类型的批处理文件代码
    /// </summary>
    /// <param name="_bat">批处理类型</param>
    /// <param name="_folder">Folder参数</param>
    /// <param name="_identifier">Identifier参数</param>
    /// <returns></returns>
    static string OnGetBatCode(ESF_enBatClassify _bat, string _folder = "", string _identifier = "")
    {
        string code = GetBatCodeTemplete(_bat);
        switch (_bat)
        {
            case ESF_enBatClassify.ClearPackageManifest:
                code = code.CompilerCSharp(new { Folder = _folder.ESF_TransPathSeparatorCharToWindowChar() });
                break;
            case ESF_enBatClassify.PlayerLog:
                code = code.CompilerCSharp(new { Folder = _folder });
                break;
            case ESF_enBatClassify.DebugProfiler:
                code = code.CompilerCSharp(new { Identifier = _identifier,AdbFolder = Path.Combine(EditorApplication.applicationContentsPath, @"PlaybackEngines\AndroidPlayer\SDK\platform-tools").ESF_TransPathSeparatorCharToWindowChar() });    
                break;
        }
        return code;
    }
    #endregion

    #region OneKeyBuildBat 一键生成批处理文件
    /// <summary>
    /// 一键生成批处理文件
    /// </summary>
    /// <returns>true:成功,false:失败</returns>
    [ESF_OneKeyBuild(ESF_enOneKeyBuildStep.BuildBat)]
    public static bool OneKeyBuildBat()
    {
        bool result = true;
        List<ESF_enBatClassify> bats = typeof(ESF_enBatClassify).ESF_ToEnum<ESF_enBatClassify>();
        string path = string.Empty;
        string code = string.Empty;
        string dir = string.Empty;
        string folder = string.Empty;
        string identifier = string.Empty;
        foreach (var key in bats)
        {
            path = GetBatFilePath(key);            
            dir = Path.GetDirectoryName(path);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }            
            switch (key)
            {
                case ESF_enBatClassify.ClearPackageManifest:
                    folder = mPackageOutputPath.ESF_TransPathSeparatorCharToWindowChar();
                    break;
                case ESF_enBatClassify.PlayerLog:
                    folder = Regex.Replace(SFGlobalSetting.persistentDataPath, "/" + Environment.UserName + "/", "/%username%/");        
                    break;
                case ESF_enBatClassify.DebugProfiler:
                    identifier = PlayerSettings.applicationIdentifier;
                    break;
            }
            code = OnGetBatCode(key, folder, identifier);
            File.WriteAllText(path, code);
        }
        ESF_Utility.ExecuteMenuAssetsRefresh();
        ESF_P.ESF_Log($"OneKeyBuildBat successed. path=>{projectSetting.xml.editorBatFolder}");
        return result;
    }
    #endregion
}
